package theFirstProject;

import theFirstProject.package1.NetPackage;
import theFirstProject.package1.SuperPackage;
import theFirstProject.package1.TalkPackage;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static theFirstProject.Demo.*;

public
class Mothod implements Serializable {
    static void doudou() {
        System.out.println("**************欢迎使用兜兜5G用户消费模拟***************");
        while (true) {
            System.out.print("输入手机号码：");
            String phone= sc.nextLine();
            Moblie mo=null;
            for (Moblie m:card.values()) {
                if (phone.equals(m.getCardNumber())) {
                    mo=m;
                    break;
                }
            }
            if(mo == null) {
                System.out.println("手机号不存在，请重新输入");
                continue;  // 改为继续循环而不是递归调用
            }
            switch (new Random().nextInt(6)+1){
                case 1:doudou1(mo);break;
                case 2:doudou2(mo);break;
                case 3:doudou3(mo);break;
                case 4:doudou4(mo);break;
                case 5:doudou5(mo);break;
                case 6:doudou6(mo);break;
            }
            Output();
            return;
        }
    }//兜兜消费
    static
    void tariffDescription() {
        try (BufferedReader br=new BufferedReader(new FileReader("d://IO//资费说明.txt"));){
            String str;
            while ((str= br.readLine())!=null){
                System.out.println(str);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }//资费说明
    static void rechargeThePhoneCredit() {
        while (true) {
            System.out.print("请输入手机号(esc退出)：");
            String phone = sc.nextLine();
            if(phone.equals("esc")){
                return;
            }else if(phone.length()!=11){
                System.out.println("手机号格式错误");
                continue;
            }
            for(Moblie m:card.values()){
                if(phone.equals(m.getCardNumber())){
                    while (true) {
                        System.out.print("输入充值金额：");
                        try {
                            double money=sc.nextDouble();
                            sc.nextLine();
                            if (money>=50) {
                                m.setMoney(m.getMoney()+money);
                                System.out.println("充值成功，当前余额："+m.getMoney());
                                Output();
                                return;
                            }else {
                                System.out.println("充值金额少于50");
                            }
                        } catch (Exception e) {
                            System.out.println("请输入有效的数字金额");
                            sc.nextLine(); // 清除无效输入
                        }
                    }
                }
            }
            System.out.println("手机号不存在");
        }
    }//话费充值
    static void login() {
        while (true) {
            System.out.print("请输入手机号(esc退出):");
            String phone = sc.nextLine();
            if (phone.equals("esc")) {
                return;
            }else if(phone.length()!=11){
                System.out.println("手机号格式错误");
                continue;
            }
            System.out.print("请输入密码：");
            String password = sc.nextLine();
            for (Moblie m : card.values()) {
                if (phone.equals(m.getCardNumber()) && password.equals(m.getPassWord())) {
                    two(phone);
                    return;
                }
            }
            System.out.println("手机号或密码错误,请重新输入");
        }
    }//登录
    static void packageChange(String phone) {
        System.out.println("**********套餐变更**********");
        Moblie mo=new Moblie();
        for (Moblie m:card.values()) {
            if (phone.equals(m.getCardNumber())) {
                mo=m;
            }
        }
        System.out.print("选择变更套餐:（1.话唠套餐、2.网虫套餐、3.超人套餐）,请输入套餐编号：");
        while (true) {
            String input = sc.nextLine();
            if("1".equals(input)) {
                if(!mo.getSericePackage().getClass().getName().contains("TalkPackage")){
                    if (mo.getMoney()>= TalkPackage.money) {
                        mo.setSericePackage(new TalkPackage());
                        mo.setConsumAmount(mo.getConsumAmount()+mo.getSericePackage().money);
                        mo.setMoney(mo.getMoney()-mo.getSericePackage().money);
                        System.out.println("更换套餐成功");
                        System.out.println("卡号："+mo.getCardNumber()+"\t"+"用户名："+mo.getUserName()+"\t"+"当前余额："+mo.getMoney()+"\n"+mo.getSericePackage());
                        break;
                    }else {
                        System.out.println("对不起，你的余额不足以支付新套餐本月资费，请充值后再办理更换套餐业务");break;
                    }
                }else {
                    System.out.println("对不起，您已经是本套餐用户，无需更改！！");break;
                }
            } else if("2".equals(input)) {
                if(!mo.getSericePackage().getClass().getName().contains("NetPackage")){
                    if (mo.getMoney()>= NetPackage.money) {
                        mo.setSericePackage(new NetPackage());
                        mo.setConsumAmount(mo.getConsumAmount()+mo.getSericePackage().money);
                        mo.setMoney(mo.getMoney()-mo.getSericePackage().money);
                        System.out.println("更换套餐成功");
                        System.out.println("卡号："+mo.getCardNumber()+"\t"+"用户名："+mo.getUserName()+"\t"+"当前余额："+mo.getMoney()+"\n"+mo.getSericePackage());
                        break;
                    }else {
                        System.out.println("对不起，你的余额不足以支付新套餐本月资费，请充值后再办理更换套餐业务");break;
                    }
                }else {
                    System.out.println("对不起，您已经是本套餐用户，无需更改！！");break;
                }
            } else if("3".equals(input)) {
                if(!mo.getSericePackage().getClass().getName().contains("SuperPackage")){
                    if (mo.getMoney()>= SuperPackage.money) {
                        mo.setSericePackage(new SuperPackage());
                        mo.setConsumAmount(mo.getConsumAmount()+mo.getSericePackage().money);
                        mo.setMoney(mo.getMoney()-mo.getSericePackage().money);
                        System.out.println("更换套餐成功");
                        System.out.println("卡号："+mo.getCardNumber()+"\t"+"用户名："+mo.getUserName()+"\t"+"当前余额："+mo.getMoney()+"\n"+mo.getSericePackage());
                        break;
                    }else {
                        System.out.println("对不起，你的余额不足以支付新套餐本月资费，请充值后再办理更换套餐业务");break;
                    }
                }else {
                    System.out.println("对不起，您已经是本套餐用户，无需更改！！");break;
                }
            } else {
                System.out.print("输入有误，请重新输入：");
            }
        }
        Output();
    }//套餐变更
    static void detailedConsumptionList(String phone) {
        try(
                BufferedWriter bw=new BufferedWriter(new FileWriter("d://IO//"+phone+"消费记录.txt")))
        {
            System.out.println("*************" + phone + "消费记录************");
            System.out.println("序号\t类型\t数据(通话(条)/上网(MB)/短信(条))");
            bw.write("*************" + phone + "消费记录************");
            bw.newLine();
            bw.write("序号\t类型\t数据(通话(条)/上网(MB)/短信(条))");
            bw.newLine();
            for (String s : consuminfo.keySet()) {
                if (s.equals(phone)) {
                    List l = consuminfo.get(s);
                    for (int i = 0; i < l.size(); i++) {
                        bw.write((i + 1) + "\t\t" + l.get(i));
                        bw.newLine();
                        System.out.println((i + 1) + ".\t\t" + l.get(i));
                    }
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }//消费记录
    static void cancelTheNetwork(String phone) {
        System.out.println("是否确认退网？（此操作不可撤回！！）y/n");
        if (sc.nextLine().equals("y")) {
            for(Moblie m:card.values()){
                if(phone.equals(m.getCardNumber())){
                    consuminfo.remove(phone);
                    File f=new File("d://IO//"+phone+"消费记录.txt");
                    f.delete();
                    card.remove(phone);
                    Output();
                    System.out.println("账号："+phone+",退网成功");
                    System.exit(0);
                }
            }
        }else return;

    }//退网
    static void marginQuery(String phone) {
        System.out.println("你的卡号："+phone+"，套餐余额剩余：");
        Moblie m=card.get(phone);
        System.out.println("通话时长："+m.getSericePackage().talkTime+"分钟");
        System.out.println("短信条数："+m.getSericePackage().smsCount+"条");
        System.out.println("上网流量："+m.getSericePackage().flow+"MB");
    }//套餐余量查询
    static void billInquiry(String phone) {
        DecimalFormat format=new DecimalFormat("#.#");
        System.out.println("你的卡号："+phone+"，当月账单如下：");
        Moblie m=card.get(phone);
        System.out.println("套餐资费："+m.getSericePackage().money+"元");
        String s=format.format(m.getConsumAmount());
        System.out.println("已使用话费："+s+"元");
        String str=format.format(m.getMoney());
        System.out.println("剩余话费："+str+"元");
        System.out.println();
        System.out.println("已经使用通话时长："+m.getRealTalkTime()+"分钟");
        System.out.println("已经使用短信条数："+m.getRealSMSCount()+"条");
        System.out.println("已经使用流量："+m.getRealFlow()+"MB");
    }//本月账单查询
    static void register() {
        Moblie m=new Moblie();
        String[] s=new String[9];
        System.out.println("选择卡号:");
        for (int i=1;i<10;i++){
            long l= 0;
            l = new Random().nextInt(100000000)+ 13900000000L;
            for (Moblie o:card.values()) {
                while (o.getCardNumber().equals(l)){
                    l = new Random().nextInt(100000000)+ 13900000000L;
                }
            }
            s[i-1]=l+"";
            System.out.print(i+". "+l +"\t");
            if(i%3==0) System.out.println();
        }
        int n= 0;

            System.out.print("选择卡号编号:");
        while (true) {
            n = 0;
            try {
                n = sc.nextInt();
                sc.nextLine();
                if(n>0&&n<10) break;
                else {
                    System.out.print("输入有误，请重新输入卡号编号:");
                }
            } catch (Exception e) {
                sc.nextLine();
                System.out.print("输入有误，请重新输入卡号编号:");
            }
        }
        m.setCardNumber(s[n-1]);

            System.out.print("选择套餐:（1.话唠套餐、2.网虫套餐、3.超人套餐）,请输入套餐编号：");
        while (true) {
            String str=sc.nextLine();
            if(str.equals("1")){
                m.setSericePackage(new TalkPackage());
                break;
            }else if(str.equals("2")){
                m.setSericePackage(new NetPackage());
                break;
            }else if(str.equals("3")){
                m.setSericePackage(new SuperPackage());
                break;
            }else System.out.print("输入有误，请重新输入：");
        }
//            switch (str){
//                case "1" : m.setSericePackage(new TalkPackage());break;
//                case "2" : m.setSericePackage(new NetPackage());break;
//                case "3" : m.setSericePackage(new SuperPackage());break;
//            }
        System.out.print("输入用户名:");
        m.setUserName(sc.nextLine());
        System.out.print("输入密码信息:");
        m.setPassWord(sc.nextLine());
        System.out.print("请输入预存的话费金额：");
        while (true) {
            double money= 0;
            try {
                money = sc.nextDouble();
                sc.nextLine();
                if(money<m.getSericePackage().money){
                    System.out.print("预存的话费金额不足以支付本月固定套餐资费，请重新输入：");
                }else {
                    m.setConsumAmount(m.getSericePackage().money);
                    m.setMoney(money-m.getSericePackage().money);
                    card.put(m.getCardNumber(),m);
                    break;
                }
            } catch (Exception e) {
                sc.nextLine();
                System.out.print("请输入有效的数字金额：");
            }
        }
        System.out.println("注册成功");
        System.out.println("卡号："+m.getCardNumber()+"\t"+"用户名："+m.getUserName()+"\t"+"当前余额："+m.getMoney()+"\n"+m.getSericePackage());
        Output();
    }//注册
    static void doudou1(Moblie mo){
        System.out.println("问候客户，谁知如此难缠，通话90分钟");
        List<ConsumInfo> list=consuminfo.computeIfAbsent(mo.getCardNumber(), k->new ArrayList<>());
        //consuminfo 是一个 Map<String, List<ConsumInfo>>，其中：
        //key 是手机号(String)
        //value 是该手机号对应的消费记录列表(List )
        //computeIfAbsent() 方法的作用是：
        //如果 mo.getCardNumber() 这个key已经存在于map中，就返回对应的value(消费记录列表)
        //如果key不存在，就执行后面的lambda表达式 k->new ArrayList<>() 创建一个新的空列表，存入map并返回
        try {
            int talkTime=talkTime(90,mo);
            mo.setRealTalkTime(mo.getRealTalkTime()+talkTime);
            if(list.size()==0) System.out.println("不存在此卡的消费记录，已添加一条消费记录");
            if(talkTime!=0) list.add(new ConsumInfo("通话",talkTime));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//场景1
    static void doudou2(Moblie mo){
        System.out.println("通知朋友换手机号，发送短信50条");
        List<ConsumInfo> list=consuminfo.computeIfAbsent(mo.getCardNumber(),k->new ArrayList<>());
        try {
            int smsCount=smsCount(50,mo);
            mo.setRealSMSCount(mo.getRealSMSCount()+smsCount);
            if(list.size()==0) System.out.println("不存在此卡的消费记录，已添加一条消费记录");
            if(smsCount!=0) list.add(new ConsumInfo("短信",smsCount));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//场景2
    static void doudou3(Moblie mo){
        System.out.println("参与环境保护实施方案问卷调查，发送短信5条");
        List<ConsumInfo> list=consuminfo.computeIfAbsent(mo.getCardNumber(),k->new ArrayList<>());
        try {
            int smsCount=smsCount(5,mo);
            mo.setRealSMSCount(mo.getRealSMSCount()+smsCount);
            if(list.size()==0) System.out.println("不存在此卡的消费记录，已添加一条消费记录");
            if(smsCount!=0) list.add(new ConsumInfo("短信",smsCount));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//场景3
    static void doudou4(Moblie mo){
        System.out.println("和父母视频通话，使用流量400MB");
        List<ConsumInfo> list=consuminfo.computeIfAbsent(mo.getCardNumber(),k->new ArrayList<>());
        try {
            int flow=flow(400,mo);
            mo.setRealFlow(mo.getRealFlow()+flow);
            if(list.size()==0) System.out.println("不存在此卡的消费记录，已添加一条消费记录");
            if(flow!=0) list.add(new ConsumInfo("流量",flow));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//场景4
    static void doudou5(Moblie mo){
        System.out.println("熬夜刷抖音，使用流量300MB");
        List<ConsumInfo> list=consuminfo.computeIfAbsent(mo.getCardNumber(),k->new ArrayList<>());
        try {
            int flow=flow(300,mo);
            mo.setRealFlow(mo.getRealFlow()+flow);
            if(list.size()==0) System.out.println("不存在此卡的消费记录，已添加一条消费记录");
            if(flow!=0) list.add(new ConsumInfo("流量",flow));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//场景5
    static void doudou6(Moblie mo){
        System.out.println("和对象煲电话粥，通话100分钟");
        List<ConsumInfo> list=consuminfo.computeIfAbsent(mo.getCardNumber(),k->new ArrayList<>());
        try {
            int talkTime=talkTime(100,mo);
            mo.setRealTalkTime(mo.getRealTalkTime()+talkTime);
            if(list.size()==0) System.out.println("不存在此卡的消费记录，已添加一条消费记录");
            if(talkTime!=0) list.add(new ConsumInfo("通话",talkTime));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//场景6
    private static
    int talkTime(int talkTime, Moblie mo){
        double D = 0.2;
        if(mo.getMoney()<=0 && mo.getSericePackage().talkTime<=0){
            System.out.println("您的手机已欠费，请前往充值，当前余额："+mo.getMoney());
        }else {
            if (talkTime <= mo.getSericePackage().talkTime) {
                mo.getSericePackage().talkTime -= talkTime;
                return talkTime;
            } else  {
                int actualTalkTime = talkTime - mo.getSericePackage().talkTime;
                double d = (actualTalkTime) * D;
                if (mo.getMoney()<d) {//余额不足
                    try {
                        talkTime = (int) (mo.getMoney() / D) + mo.getSericePackage().talkTime;
                        mo.setConsumAmount(mo.getConsumAmount() + mo.getMoney());
                        mo.setMoney(0);
                        mo.getSericePackage().talkTime = 0;
                        throw new Exception("当前已经通话" + talkTime + "分钟，余额不足，本次通话结束");
                    } catch (Exception e) {
                        StringWriter sw = new StringWriter();
                        e.printStackTrace(new PrintWriter(sw));
                        System.out.println("\u001B[31m" + sw.toString() + "\u001B[0m");
                    } finally {
                        return talkTime;
                    }
                } else  {//余额足够
                    mo.setConsumAmount(mo.getConsumAmount() + d);
                    mo.setMoney(mo.getMoney() - d);
                    mo.getSericePackage().talkTime = 0;
                    return talkTime;
                }
            }
        }
        return talkTime;
    }//判断通话费用
    private static int smsCount(int smsCount,Moblie mo) {
        double D = 0.1;
        if(mo.getMoney()<0 && mo.getSericePackage().smsCount<0){
            System.out.println("您的手机已欠费，请前往充值，当前余额："+mo.getMoney());
        }else {
            if (smsCount <= mo.getSericePackage().smsCount) {
                mo.getSericePackage().smsCount -= smsCount;
                return smsCount;
            } else  {
                int actualSmsCount = smsCount - mo.getSericePackage().smsCount;
                double d = (actualSmsCount) * D;
                if (mo.getMoney()<d) {//余额不足
                    try {
                        smsCount = (int) (mo.getMoney() / D) + mo.getSericePackage().smsCount;
                        mo.setConsumAmount(mo.getConsumAmount() + mo.getMoney());
                        mo.setMoney(0);
                        mo.getSericePackage().smsCount = 0;
                        throw new Exception("当前已经发送短信" + smsCount + "条，余额不足，本次短信发送结束");
                    } catch (Exception e) {
                        StringWriter sw = new StringWriter();
                        e.printStackTrace(new PrintWriter(sw));
                        System.out.println("\u001B[31m" + sw.toString() + "\u001B[0m");

                    } finally {
                        return smsCount;
                    }
                } else  {//余额足够
                    mo.setConsumAmount(mo.getConsumAmount() + d);
                    mo.setMoney(mo.getMoney() - d);
                    mo.getSericePackage().smsCount = 0;
                    return smsCount;
                }
            }
        }
        return smsCount;
    }//判断短信费用
    private static int flow(int flow,Moblie mo){
        double D = 0.1;
        if(mo.getMoney()<0 && mo.getSericePackage().flow<0){
            System.out.println("您的手机已欠费，请前往充值，当前余额："+mo.getMoney());
        }else {
            if (flow <= mo.getSericePackage().flow) {
                mo.getSericePackage().flow -= flow;
                return flow;
            } else  {
                int actualFlow = flow - mo.getSericePackage().flow;
                double d = (actualFlow) * D;
                if (mo.getMoney()<d) {//余额不足
                    try {
                        flow = (int) (mo.getMoney() / D) + mo.getSericePackage().flow;
                        mo.setConsumAmount(mo.getConsumAmount() + mo.getMoney());
                        mo.setMoney(0);
                        mo.getSericePackage().flow = 0;
                        throw new Exception("当前已经使用流量" + flow + "MB，余额不足，本次流量使用结束");
                    } catch (Exception e) {
                        StringWriter sw = new StringWriter();
                        e.printStackTrace(new PrintWriter(sw));
                        System.out.println("\u001B[31m" + sw.toString() + "\u001B[0m");
                    } finally {
                        return flow;
                    }
                } else  {//余额足够
                    mo.setConsumAmount(mo.getConsumAmount() + d);
                    mo.setMoney(mo.getMoney() - d);
                    mo.getSericePackage().flow = 0;
                    return flow;
                }
            }
        }
        return flow;
    }//判断流量费用
}
